/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package form;

import core.event.DelegateEvent;
import core.event.DelegateListener;
import io.resource.DataRef;
import io.resource.ResourceDelegate;
import io.resource.ResourceReader;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import tools.WorldCanvasTool;

/**
 *
 * @author Robert A. Cherry
 */
public class StatusBar extends javax.swing.JPanel implements DelegateListener {

    // Variable Declaration
    // Java Swing Classes
    private JLabel referenceJLabel;
    // Java Native Classes
    private ImageIcon iconConflict;
    private ImageIcon iconToolOff;
    private ImageIcon iconToolOn;
    private ImageIcon iconInfo;
    // Project Classes
    private FaustEditor editor;
    private ResourceDelegate delegate;
    private WorldCanvasTool tool;
    // End of Variable Declaration

    public StatusBar(FaustEditor editor, ResourceDelegate delegate) {
        initComponents();

        // Set values
        this.editor = editor;
        this.delegate = delegate;

        init();
    }

    private void init() {

        final Class closs = getClass();

        //
        final Dimension dimension = new Dimension(editor.getPreferredSize().width, 24);
        final Dimension labelDimension = new Dimension(196, 24);
        final Dimension fillerDimension = new Dimension(8, 24);

        //
        setPreferredSize(dimension);
        setMaximumSize(dimension);
        setMinimumSize(dimension);
        setSize(dimension);

        //
        referenceJLabel = new JLabel();
        referenceJLabel.setHorizontalAlignment(SwingConstants.TRAILING);
        referenceJLabel.setPreferredSize(labelDimension);
        referenceJLabel.setMaximumSize(labelDimension);
        referenceJLabel.setMinimumSize(labelDimension);

        //
        delegate.addDelegateListener(this);

        //
        tool = editor.getCanvas().getTool();

        //
        iconInfo = ResourceReader.readClassPathIcon(closs, "/icons/icon-info16.png");
        iconConflict = ResourceReader.readClassPathIcon(closs, "/icons/icon-warning24.png");
        iconToolOn = ResourceReader.readClassPathIcon(closs, "/icons/icon-visible-on18.png");
        iconToolOff = ResourceReader.readClassPathIcon(closs, "/icons/icon-visible-off18.png");

        // Default
        conflictionJButton.setIcon(iconConflict);
        canvasJButton.setIcon(iconToolOn);

        //
        if (tool != null) {

            //
            canvasJButton.setIcon(tool.isVisible() ? iconToolOn : iconToolOff);
        }

        //
        add(Box.createHorizontalGlue());
        add(referenceJLabel);
        add(new Box.Filler(fillerDimension, fillerDimension, fillerDimension));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        canvasJButton = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        conflictionJButton = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(32767, 24));
        setMinimumSize(new java.awt.Dimension(32767, 24));
        setPreferredSize(new java.awt.Dimension(32767, 24));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        canvasJButton.setToolTipText("Toggle canvas tool visibility");
        canvasJButton.setContentAreaFilled(false);
        canvasJButton.setFocusPainted(false);
        canvasJButton.setMaximumSize(new java.awt.Dimension(24, 24));
        canvasJButton.setMinimumSize(new java.awt.Dimension(24, 24));
        canvasJButton.setPreferredSize(new java.awt.Dimension(24, 24));
        canvasJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canvasJButtonActionPerformed(evt);
            }
        });
        add(canvasJButton);
        add(filler1);

        conflictionJButton.setContentAreaFilled(false);
        conflictionJButton.setFocusPainted(false);
        conflictionJButton.setMaximumSize(new java.awt.Dimension(24, 24));
        conflictionJButton.setMinimumSize(new java.awt.Dimension(24, 24));
        conflictionJButton.setPreferredSize(new java.awt.Dimension(24, 24));
        conflictionJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                conflictionJButtonActionPerformed(evt);
            }
        });
        add(conflictionJButton);
    }// </editor-fold>//GEN-END:initComponents

    private void conflictionJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_conflictionJButtonActionPerformed

        // Must have loaded resources of some kind
        if (delegate.isLoaded()) {

            // Must have detected conflictions
            if (delegate.detectConflictions().length > 0) {
                final ConflictMonitor monitor = new ConflictMonitor(editor, delegate, true);
                monitor.setLocationRelativeTo(editor);
                monitor.setVisible(true);
            }
        }
    }//GEN-LAST:event_conflictionJButtonActionPerformed

    private void canvasJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canvasJButtonActionPerformed

        //
        tool = editor.getCanvas().getTool();

        //
        if (tool != null) {

            // Switch
            tool.setVisible(!tool.isVisible());

            // Determine
            canvasJButton.setIcon(tool.isVisible() ? iconToolOn : iconToolOff);
        }
    }//GEN-LAST:event_canvasJButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton canvasJButton;
    private javax.swing.JButton conflictionJButton;
    private javax.swing.Box.Filler filler1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void referenceAdded(DelegateEvent evt) {

        //
        final DataRef source = (DataRef) evt.getSource();

        //
        referenceJLabel.setText("Loading Resource: " + source.getEditorName());

        //
        javax.swing.Timer timer = new javax.swing.Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {

                // Clear the text
                referenceJLabel.setText("");
                referenceJLabel.setIcon(null);
            }
        }) {
        };
        timer.setRepeats(false);
        timer.start();

        //
        if (delegate.detectConflictions().length > 0) {
            conflictionJButton.setEnabled(true);
            conflictionJButton.setIcon(iconConflict);
        } else {
            conflictionJButton.setEnabled(false);
            conflictionJButton.setIcon(null);
        }
    }

    @Override
    public void referenceRemoved(DelegateEvent evt) {

        //
        if (delegate.detectConflictions().length > 0) {
            conflictionJButton.setEnabled(true);
            conflictionJButton.setIcon(iconConflict);
        } else {
            conflictionJButton.setEnabled(false);
            conflictionJButton.setIcon(null);
        }
    }

    @Override
    public void dataLoaded(DelegateEvent evt) {

        //
        referenceJLabel.setIcon(iconInfo);
        referenceJLabel.setText("Resource Check Complete");

        //
        if (delegate.detectConflictions().length > 0) {
            conflictionJButton.setEnabled(true);
            conflictionJButton.setIcon(iconConflict);
        } else {
            conflictionJButton.setEnabled(false);
            conflictionJButton.setIcon(null);
        }
    }

    @Override
    public void packageAdded(DelegateEvent evt) {
        //
    }

    @Override
    public void packageRemoved(DelegateEvent evt) {
        //
    }
}
